const md = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputData: {
      input_value: "",//输入框的初始内容
      value_length: 0,//输入框密码位数
      isNext: true,//是否有下一步的按钮
      get_focus: true,//输入框的聚焦状态
      focus_class: true,//输入框聚焦样式
      value_num: [1, 2, 3, 4, 5, 6],//输入框格子数
      height: "80rpx",//输入框高度
      width: "560rpx",//输入框宽度
      see: false,//是否明文展示
      interval: false,//是否显示间隔格子
      maxlength: '6'
    },
    val_arr: [],
    input_value: '',
    model: 1,
    payno: 123456,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      userinfo: wx.getStorageSync('userinfo'),
      payno: options.payno,
      mebid: wx.getStorageSync('userinfo').MebID
    });
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onShow: function () {
    var that = this;
    // 获取时间戳
    var apinameIsMeb = "/Action/ZKPay/VerfiyPayment";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        PayNo: that.data.payno
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          if (res.data.data.Status == "N") {
            var paystr ="支付";
            if(res.data.data.PayType == "P"){ 
              paystr ="接收";
              
            }
            var hasmoney=0;
            if(res.data.data.Money>0){
                hasmoney=1;
            }
            
            that.setData({
              money: res.data.data.Money,
              detail: res.data.data.Detail,
              mebname: res.data.data.MebName,
              paytype: res.data.data.PayType,
              paystr:paystr,
              hasmoney:hasmoney
            })
            return;
          } else {
            wx.showModal({
              title: '',
              content: "扫描的码已过期",
              success: function (res) {
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          }
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
            success: function (res) {
              wx.navigateBack({
                delta: 1,
              })
            }
          })
          
        }
      }
    })

  },

  valueSix(e) {
    console.log(e.detail);
    var that = this;
    that.setData({
      paypassword: e.detail
    })
  },

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    var pwd = that.data.paypassword;
    // return;
    if (pwd.length != 6) {
      wx.showModal({
        title: '',
        content: '支付密码必须为6位数字',
      })
      return;
    }

    // 获取时间戳
    var apinameIsMeb = "/Action/ZKPay/DonePayment";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        PayNo: that.data.payno,
        MebID: that.data.mebid,
        Detail: that.data.detail,
        PayPsw: md.hexMD5(pwd),
        Money: that.data.money,
        AccID: wx.getStorageSync('depid')
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.redirectTo({
            url: '/pages/success/success?name=' + res.data.data.MebName + "&photo=" + res.data.data.PhotoUrl + "&money=" + that.data.money + "&paytype=" + that.data.paystr + "&state=" + res.data.data._BOState,
          })
        }else{
          wx.showModal({
            title: '',
            content: res.data.msg,
          })
        }
      }
    })

  },
  cancer: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  ok: function () {
    
    var that =this;
    if(that.data.paytype =="P"){
      var apinameIsMeb = "/Action/ZKPay/DonePayment";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          PayNo: that.data.payno,
          MebID: that.data.mebid,
          Detail: that.data.detail,
          // PayPsw: md.hexMD5(pwd),
          Money: that.data.money,
          PayPsw:'',
          AccID: wx.getStorageSync('depid')
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.code == 0) {
            wx.redirectTo({
              url: '/pages/success/success?name=' + res.data.data.MebName + "&photo=" + res.data.data.PhotoUrl + "&money=" + that.data.money + "&paytype=" + that.data.paystr + "&state=" + res.data.data._BOState,
            })
          }else{
            wx.showModal({
              title: '',
              content: res.data.msg,
            })
            return;
          }
        }
      })
    }else{
      if(that.data.money.length == 0){
        wx.showModal({
          title: '',
          content: '请输入正确的支付金额！',
        })
      }
      that.setData({
        model: 0
      })
    }
  },
  getpwd: function (e) {
    this.setData({
      money: e.detail.value
    })
  }
})